package top.haijunit.sample.navi.utils;

import lombok.experimental.UtilityClass;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常工具类
 *
 * @author zhanghaijun
 * @date 2024/4/20 10:01
 * @description [一句话描述该类的功能]
 */
@UtilityClass
public class ExceptionUtils {

    /**
     * Exception出错的栈信息转成字符串
     * 用于打印到日志中
     */
    public String errorInfoToString(Throwable e) {
        try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            return sw.toString();
        } catch (Exception exception) {
            throw new RuntimeException(exception.getMessage(), exception);
        }
    }

}
